package com.gym.core.common;

import java.util.Locale;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * just copy
 * @author 532002108@qq.com
 * @date 2015-9-12 下午12:37:02
 */
public class ConfigUtil {
	private final static Logger logger = LoggerFactory.getLogger(ConfigUtil.class);
	private static ResourceBundle rb = null;
	public static final Locale locale = new Locale("zh", "CN");
	private static boolean isProd = false;
	private static boolean isDev = true;

	static {
		try {
			rb = ResourceBundle.getBundle("config", locale);
			processSystemType();
		} catch (Exception e) {
			logger.error("error", e);
		}
	}

	private static void processSystemType() {
		String systemType = null;
		if (rb.containsKey("system.type")) {
			systemType = rb.getString("system.type");
		}

		if (systemType == null) {
			systemType = "dev";
		}

		if (systemType.startsWith("prod")) {
			isProd = true;
			isDev = false;
		} else {
			isProd = false;
			isDev = true;
		}
		logger.warn("SystemConfig: isProd=" + isProd + ", isDev=" + isDev);
	}

	public static String getStringItem(String item, String defaultValue) {
		String value = null;
		if (rb != null) {
			try {
				value = rb.getString(item.trim());
			} catch (Exception e) {
			}
		}
		if (value == null) {
			return defaultValue;
		}
		return value.trim();
	}

	public static int getIntItem(String item, String defaultValue) {
		int i = 0;
		String value = getStringItem(item, defaultValue);
		try {
			i = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			logger.error("item=" + item + ", defaultValue=" + defaultValue, e);
		}
		return i;
	}

	public static boolean getBooleanItem(String item, boolean defaultValue) {
		boolean b = false;
		String value = getStringItem(item, Boolean.valueOf(defaultValue)
				.toString());
		if (value != null && value.equalsIgnoreCase("true")) {
			b = true;
		}
		return b;
	}

	public static boolean isProd() {
		return isProd;
	}

	public static boolean isDev() {
		return isDev;
	}

}
